package com.coollang.smartbra.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.beans.MyDeviceBean;

import java.util.List;

/**
 * Created by Administrator on 2016/11/12 0012.
 */

public class MyDeviceAdapter extends BaseAdapter {
    private List<MyDeviceBean> otherList;
    private Context context;

    public MyDeviceAdapter(Context context, List<MyDeviceBean> otherList) {
        this.context = context;
        this.otherList = otherList;
    }

    public void notify(List<MyDeviceBean> otherList) {
        this.otherList = otherList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return otherList.size();
    }

    @Override
    public Object getItem(int position) {
        return otherList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private ViewHolder holder;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_mydevice, null);
            holder = new ViewHolder();
            holder.tv_index = (TextView) convertView.findViewById(R.id.tv_index);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.iv_bind = (ImageView) convertView.findViewById(R.id.iv_bind);
            holder.tv_connected = (TextView) convertView.findViewById(R.id.tv_connected);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_index.setText("设备" + (position + 1));
        holder.tv_name.setText(otherList.get(position).getName());
        if (otherList.get(position).isBind()) {
            holder.iv_bind.setVisibility(View.VISIBLE);
            holder.tv_connected.setVisibility(View.GONE);
        } else {
            holder.iv_bind.setVisibility(View.GONE);
            holder.tv_connected.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    private class ViewHolder {
        public ImageView iv_bind;
        public TextView tv_name;
        public TextView tv_connected;
        public TextView tv_index;
    }
}
